package cn.itcast.oa.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.oa.base.DaoSupportImpl;
import cn.itcast.oa.domain.Forum;
import cn.itcast.oa.domain.Topic;
import cn.itcast.oa.service.TopicService;

@Service
@Transactional
public class TopicServiceImpl extends DaoSupportImpl<Topic> implements TopicService {

	public void save(Topic topic) {
		// 1、设置属性并且保存
		topic.setType(Topic.getTypeNormal()); // 默认普通帖
		topic.setLastReply(null); // 默认空
		topic.setReplyCount(0); // 默认0
		topic.setLastUpdateTime(topic.getPostTime());
		getSession().save(topic);

		// 2、维护相关的特殊属性
		Forum forum = topic.getForum();
		forum.setTopicCount(forum.getTopicCount() + 1); // 主题数量
		forum.setArticleCount(forum.getArticleCount() + 1); // 文章数量
		forum.setLastTopic(topic); // 最后发表时间
		getSession().save(forum);
	}

}
